<?php
class EnrolledstudentsController extends AppController {

	var $name = 'Enrolledstudents';
	var $helpers = array('Html', 'Form');

		function index() {
			// выводим страницу с интерфейсом работы ответственного секретаря
			// для вывода формы генерации рейтигра по факультету специальности
			$this->loadModel('Facultet');
			$facultets = $this->Facultet->find('list');
			$this->loadModel('Learningform');
			$learningforms = $this->Learningform->find('list');
			$this->loadModel('Learningtype');
			$learningtypes = $this->Learningtype->find('list');
			//, 'professions'
			$this->set(compact('learningtypes', 'learningforms', 'facultets'));
		}
		
		/**
		* @author Adm
		*	для ajax обновляет select с профессиями по выбранному факультету
		*/
		function update_profession() {
			$this->loadModel('Profession');
			if(!empty($this->data['Enrolledstudent']['facultet_id'])) {
				$facultet_id = $this->data['Enrolledstudent']['facultet_id'];
				$options = $this->Profession->find('list', array('conditions' => array ('Profession.facultet_id' => $facultet_id)));
				$this->set(compact('options'));
			}
		}
		
		
function facultetrating() {
		$facultet_id = $this->data['Enrolledstudent']['facultet_id'];
		$this->set('facultet_id', $facultet_id);
		$profession_id = $this->data['Enrolledstudent']['profession_id'];
		$this->set('profession_id', $profession_id);
		$learningform_id = $this->data['Enrolledstudent']['learningform_id'];
		$this->set('learningform_id', $learningform_id);
		$learningtype_id = $this->data['Enrolledstudent']['learningtype_id'];
		$this->set('learningtype_id', $learningtype_id);
		
		$this->set('facultet_id, profession_id, learningform_id, learningtype_id');

		$this->loadModel('Facultet');
		$facultets = $this->Facultet->find('list');
		$this->loadModel('Learningform');
		$learningforms = $this->Learningform->find('list');
		$this->loadModel('Learningtype');
		$learningtypes = $this->Learningtype->find('list');
		//, 'professions'
		$this->set(compact('learningtypes', 'learningforms', 'facultets'));
		
		$this->loadModel('Profession');
		$professions = $this->Profession->find('list');
		$this->set(compact('professions'));
		$profession = $this->Profession->find('all', array('conditions' => array('Profession.id' => $profession_id)));		

		// получаем id предметов по специальности
		$subjects_ids = $this->Profession->SubjectsProfession->find('all', array('conditions' => array('SubjectsProfession.profession_id' => $profession_id)));

		$profession_subjects = array();
		foreach ($subjects_ids as $subject):
		array_push($profession_subjects, $subject['SubjectsProfession']['subject_id']);
		endforeach;		

		$this->set('subjects_ids', $profession_subjects);
		
			
		$this->loadModel('Targetingintake');
		$this->loadModel('Abiturient');
		
		$this->loadModel('Region');
		$regions = $this->Region->find('list');
		$this->set('regions', $regions);
		
		$this->loadModel('Subject');
		$subjects = $this->Subject->find('list');
		$this->set('subjects', $subjects);
		
		$this->loadModel('Tgintakeorg');
		$tgintakeorgs = $this->Tgintakeorg->find('all' , array('recursive' => -1));	
		$tgintakeorgslist = $this->Tgintakeorg->find('list');
		$this->set('tgintakeorgslist', $tgintakeorgslist);

			// массив id фабитиентов исключаемых из дальнейшего поиска
			$excluded_ids = array();
			// id абитуриентов сгрупированных по огранизациям выдавшим целевое отсортированным по баллам
			$celevoe_tgintakeorgs_abiturients = array();
			foreach ($tgintakeorgs as $tgintakeorg):
				
				$tgabiturients = $this->Targetingintake->find('all', array('conditions' => array('Targetingintake.profession_id' => $profession_id,
					                 															     'Targetingintake.learningform_id' => $learningform_id, 
					                 															     'Targetingintake.learningtype_id' => $learningtype_id,
																									 'Targetingintake.tgintakeorg_id' => $tgintakeorg['Tgintakeorg']['id']),
																'recursive' => -1
																));
				$abiturients_ids = array();
				
				foreach ($tgabiturients as $abiturient):
						array_push($abiturients_ids, $abiturient['Targetingintake']['abiturient_id']);
						array_push($excluded_ids, $abiturient['Targetingintake']['abiturient_id']);
				endforeach;

				// выбираем инфо по абитуриентам

				$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $abiturients_ids,
																												'Abiturient.prepared' => 1,
																												'Abiturient.approved' => 1,
																												'Abiturient.canceled' => 0,
																												//'Abiturient.enrolled' => 0,
																												),
													'recursive' => 1,
													'fields' => array ('Abiturient.id', 
																		'Abiturient.lastname', 
																		'Abiturient.firstname', 
																		'Abiturient.surname', 
																		'Abiturient.originaleducationdoc',)));		

				// считаем сумму баллов по специальности
				$score_total_abiturients = array(); // к данным абитуриента добавлен елемент Score_total
				foreach ($abiturients as $abiturient):
					// считаем  сумму балов по специальности
					$score_total = 0;
					foreach ($subjects_ids as $subject_id):
						foreach ($abiturient['Result'] as $result):
						if ($subject_id['SubjectsProfession']['subject_id'] == $result['subject_id']){
							$score_total += $result['score'];
						}
						endforeach;
					endforeach;
					$abiturient['Score_total'] = $score_total;
					array_push($score_total_abiturients, $abiturient);
				endforeach;
				// начинаем разбирать абитуриентов в  массив с сортровкой по рейтингу
				$arr_region_scoretotal_ids = array();
				foreach ($score_total_abiturients as $abiturient):
				$arr_region_scoretotal_ids[$abiturient['Abiturient']['id']] = $abiturient['Score_total'];
				endforeach;
				// сортируем массив целивиков по убыванию	Score_total
				arsort($arr_region_scoretotal_ids);		

				array_push($celevoe_tgintakeorgs_abiturients, array('tgintakeorg_id' =>  $tgintakeorg['Tgintakeorg']['id'], 'sortedabiturients_ids' => $arr_region_scoretotal_ids));
			endforeach; // tgintakeorgs

						$this->set('celevoe_tgintakeorgs_abiturients',$celevoe_tgintakeorgs_abiturients);
			
			
			/*
			 * вы вибираем льгоников
			 */
			$this->loadModel('Privilege');
			$this->loadModel('Selectedprofession');
						
			$privileges = $this->Privilege->find('all', array('recursive' => -1));
			$privelegeslist = $this->Privilege->find('list');
			$this->set('privilegeslist',$privelegeslist);
			
			$privileges_abiturients = array();
			foreach ($privileges as $privilege):
			
				$privilege_abiturients = array();
			
				$id_not_in_ids = array('NOT' => array('Selectedprofession.abiturient_id' => $excluded_ids));
				$selectedprofession_abiturients_ids = $this->Selectedprofession->find('all', array('conditions' => array( $id_not_in_ids,
																										 'Selectedprofession.profession_id' => $profession_id,
						                 															     'Selectedprofession.learningform_id' => $learningform_id, 
						                 															     'Selectedprofession.learningtype_id' => $learningtype_id,),
																	'recursive' => -1,
																	'fields' => array('Selectedprofession.abiturient_id')
																	));
				$abiturients_ids = array();
				foreach ($selectedprofession_abiturients_ids as $abiturient):
							array_push($abiturients_ids, $abiturient['Selectedprofession']['abiturient_id']);
				endforeach;
				
				$this->loadModel('AbiturientsPrivilege');
				$selectedprofession_privilege_ids = $this->AbiturientsPrivilege->find('all', array('conditions' => array('AbiturientsPrivilege.abiturient_id' => $abiturients_ids, 
																															'AbiturientsPrivilege.privilege_id' => $privilege['Privilege']['id']),
																						'recursive' => -1,
																						));
				// debug($selectedprofession_privileges_ids);
				$abiturients_ids = array();			
				foreach ($selectedprofession_privilege_ids as $abiturient_id):
					array_push($abiturients_ids, $abiturient_id['AbiturientsPrivilege']['abiturient_id']);		
					array_push($excluded_ids, $abiturient_id['AbiturientsPrivilege']['abiturient_id']);					
				endforeach;
				

				// выбираем инфо по абитуриентам
				
				$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $abiturients_ids,
																																'Abiturient.prepared' => 1,
																																'Abiturient.approved' => 1,
																																'Abiturient.canceled' => 0,
																																//'Abiturient.enrolled' => 0,
																																),
																	'recursive' => 1,
																	'fields' => array ('Abiturient.id', 
																						'Abiturient.lastname', 
																						'Abiturient.firstname', 
																						'Abiturient.surname', 
																						'Abiturient.originaleducationdoc')));		
				
				// считаем сумму баллов по специальности
				$score_total_abiturients = array(); // к данным абитуриента добавлен елемент Score_total
				foreach ($abiturients as $abiturient):
				// считаем  сумму балов по специальности
				$score_total = 0;
				foreach ($subjects_ids as $subject_id):
				foreach ($abiturient['Result'] as $result):
				if ($subject_id['SubjectsProfession']['subject_id'] == $result['subject_id']){
					$score_total += $result['score'];
				}
				endforeach;
				endforeach;
				$abiturient['Score_total'] = $score_total;
				array_push($score_total_abiturients, $abiturient);
				endforeach;
				// начинаем разбирать абитуриентов в  массив с сортровкой по рейтингу
				$arr_privilege_scoretotal_ids = array();
				foreach ($score_total_abiturients as $abiturient):
				$arr_privilege_scoretotal_ids[$abiturient['Abiturient']['id']] = $abiturient['Score_total'];
				endforeach;
				// сортируем массив целивиков по убыванию	Score_total
				arsort($arr_privilege_scoretotal_ids);
				
				array_push($privileges_abiturients, array('privilege_id' =>  $privilege['Privilege']['id'], 'sortedabiturients_ids' => $arr_privilege_scoretotal_ids));				
				
			endforeach;	
			
			// debug($privileges_abiturients);
			$this->set('privileges_abiturients',$privileges_abiturients);
			
			/*
			 * выбираем абитуриентов для простого рейтинга
			 */
			
			$plainrating_abiturients = array ();
			$id_not_in_ids = array('NOT' => array('Selectedprofession.abiturient_id' => $excluded_ids));
			$selectedprofession_abiturients_ids = $this->Selectedprofession->find('all', array('conditions' => array( $id_not_in_ids,
																													 'Selectedprofession.profession_id' => $profession_id,
									                 															     'Selectedprofession.learningform_id' => $learningform_id, 
									                 															     'Selectedprofession.learningtype_id' => $learningtype_id,),
																				'recursive' => -1,
																				'fields' => array('Selectedprofession.abiturient_id')
			));
			$abiturients_ids = array();
			foreach ($selectedprofession_abiturients_ids as $abiturient):
			array_push($abiturients_ids, $abiturient['Selectedprofession']['abiturient_id']);
			array_push($excluded_ids, $abiturient['Selectedprofession']['abiturient_id']);			
			endforeach;
						
			// выбираем инфо по абитуриентам
			
			$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $abiturients_ids,
																										'Abiturient.prepared' => 1,
																										'Abiturient.approved' => 1,
																										'Abiturient.canceled' => 0,
																										//'Abiturient.enrolled' => 0,
																										),
																				'recursive' => 1,
																				'fields' => array ('Abiturient.id', 
																									'Abiturient.lastname', 
																									'Abiturient.firstname', 
																									'Abiturient.surname', 
																									'Abiturient.originaleducationdoc',)));		
			
			// считаем сумму баллов по специальности
			$score_total_abiturients = array(); // к данным абитуриента добавлен елемент Score_total
			foreach ($abiturients as $abiturient):
			// считаем  сумму балов по специальности
			$score_total = 0;
			foreach ($subjects_ids as $subject_id):
			foreach ($abiturient['Result'] as $result):
			if ($subject_id['SubjectsProfession']['subject_id'] == $result['subject_id']){
				$score_total += $result['score'];
			}
			endforeach;
			endforeach;
			$abiturient['Score_total'] = $score_total;
			array_push($score_total_abiturients, $abiturient);
			endforeach;
			
			// начинаем разбирать абитуриентов в  массив с сортровкой по рейтингу
			$plainrating_abiturients = array();
			foreach ($score_total_abiturients as $abiturient):
			$plainrating_abiturients[$abiturient['Abiturient']['id']] = $abiturient['Score_total'];
			endforeach;
			// сортируем массив целивиков по убыванию	Score_total
			arsort($plainrating_abiturients);
			
			$this->set('plainrating_abiturients',$plainrating_abiturients);
			
			$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $excluded_ids,
																																'Abiturient.prepared' => 1,
																																'Abiturient.approved' => 1,
																																'Abiturient.canceled' => 0,
																																//'Abiturient.enrolled' => 0,
																																),
																	'recursive' => 1,
																	'fields' => array ('Abiturient.id', 
																						'Abiturient.lastname', 
																						'Abiturient.firstname', 
																						'Abiturient.surname', 
																						'Abiturient.originaleducationdoc',
																						'Abiturient.region_id')));
			$this->set('abiturients', $abiturients)	;		
		// $this->render('/','blank','facultetrating');
	}	
	
	/*
	 * зачисляем выбранных студентов на специальность форму тип обучения
	 */
	function enrollstudent (){
		// debug($this->data);
		$abiturients = $this->data['abiturients'];
		$profession_id = $this->data['profession_id'];
		$learningform_id = $this->data['learningform_id'];
		$learningtype_id = $this->data['learningtype_id'];
		
		$abiturients_ids = array();
		
		$this->loadModel('Abiturient');
		// выбираем id абитуриентов для зачисления
		foreach($abiturients as $key => $value):
		if ($value){
			
			array_push($abiturients_ids, $value);
			
			$abiturient = $this->Enrolledstudent->find('all', array('conditions' => array('Enrolledstudent.abiturient_id' => $value)));
			
			if ($abiturient) {
				$this->Enrolledstudent->del($abiturient[0]['Enrolledstudent']['id']);
			}
			
			$this->Enrolledstudent->create();
			
			$newdata = array('Enrolledstudent' => array('abiturient_id' => $value,
														'profession_id' => $profession_id,
														'learningform_id' => $learningform_id,
														'learningtype_id' => $learningtype_id,
														));
			
			$this->Enrolledstudent->save($newdata);
			
			$this->Abiturient->id = $value;
			
			$this->Abiturient->saveField('enrolled', '1');
		}
		endforeach;		
		
		$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $abiturients_ids,),
																			'recursive' => -1,
																			'fields' => array ('Abiturient.id', 
																								'Abiturient.lastname', 
																								'Abiturient.firstname', 
																								'Abiturient.surname')));
		$this->set('abiturients', $abiturients)	;		
		
	}
	
	/*
	 * список зачисленных студентов для сброса обратно  в рейтингы
	 */
	function listenrolled(){
		$profession_id = $this->data['Enrolledstudent']['profession_id'];
		$learningform_id = $this->data['Enrolledstudent']['learningform_id'];
		$learningtype_id = $this->data['Enrolledstudent']['learningtype_id'];

		$enrolledabiturients = $this->Enrolledstudent->find('all', array('conditions' => array('Enrolledstudent.profession_id' => $profession_id,
																							'Enrolledstudent.learningform_id' => $learningform_id,
																							'Enrolledstudent.learningtype_id' => $learningtype_id,
																								)));
		$this->loadModel('Abiturient');
		$enrolled_ids = array();
		foreach($enrolledabiturients as $abiturient):
		array_push($enrolled_ids, $abiturient['Enrolledstudent']['abiturient_id']);
		endforeach;
		$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $enrolled_ids,),
																					'recursive' => -1,
																					'order' => 'Abiturient.lastname',
																					'fields' => array ('Abiturient.id', 
																										'Abiturient.lastname', 
																										'Abiturient.firstname', 
																										'Abiturient.surname')));
		$this->set('abiturients', $abiturients)	;		
	}
	/*
	 * сброс зачисленных студентов обратно  в рейтингы
	 */
	function unenrollstudent(){
		$abiturients = $this->data['abiturients'];
		$abiturients_ids = array();
		
		$this->loadModel('Abiturient');
		// выбираем id абитуриентов для зачисления
		foreach($abiturients as $key => $value):
		if ($value){
				
			array_push($abiturients_ids, $value);

			$abiturient = $this->Enrolledstudent->find('all', array('conditions' => array('Enrolledstudent.abiturient_id' => $value)));
			
			$this->Enrolledstudent->del($abiturient[0]['Enrolledstudent']['id']);
				
			$this->Abiturient->id = $value;
				
			$this->Abiturient->saveField('enrolled', '0');
		}
		endforeach;
		
		$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $abiturients_ids,),
																					'recursive' => -1,
																					'fields' => array ('Abiturient.id', 
																										'Abiturient.lastname', 
																										'Abiturient.firstname', 
																										'Abiturient.surname')));
		$this->set('abiturients', $abiturients)	;		
	}
	/*
	 * выводит списки зачисленных абитуриентов по факультету форме и типу обучения
	 */
	
	function listenrolledfacultet(){

		$facultet_id = $this->data['Enrolledstudent']['facultet_id'];
		$this->set('facultet_id', $facultet_id);
		$profession_id = $this->data['Enrolledstudent']['profession_id'];
		$this->set('profession_id', $profession_id);
		$learningform_id = $this->data['Enrolledstudent']['learningform_id'];
		$this->set('learningform_id', $learningform_id);
		$learningtype_id = $this->data['Enrolledstudent']['learningtype_id'];
		$this->set('learningtype_id', $learningtype_id);
		
		$this->set('facultet_id, profession_id, learningform_id, learningtype_id');

		$this->loadModel('Facultet');
		$facultets = $this->Facultet->find('list');
		$this->loadModel('Learningform');
		$learningforms = $this->Learningform->find('list');
		$this->loadModel('Learningtype');
		$learningtypes = $this->Learningtype->find('list');
		//, 'professions'
		$this->set(compact('learningtypes', 'learningforms', 'facultets'));
		
		$this->loadModel('Privilege');
		$privelegeslist = $this->Privilege->find('list');
		$this->set('privilegeslist',$privelegeslist);

		$this->loadModel('Profession');
		$professions = $this->Profession->find('list');
		$this->set(compact('professions'));
		
		// получаем id предметов по специальности
		$subjects_ids = $this->Profession->SubjectsProfession->find('all', array('conditions' => array('SubjectsProfession.profession_id' => $profession_id)));
		
		$profession_subjects = array();
		foreach ($subjects_ids as $subject):
		array_push($profession_subjects, $subject['SubjectsProfession']['subject_id']);
		endforeach;
		
		$this->set('subjects_ids', $profession_subjects);
		
		$this->loadModel('Region');
		$regions = $this->Region->find('list');
		$this->set('regions', $regions);
		
		$this->loadModel('Subject');
		$subjects = $this->Subject->find('list');
		$this->set('subjects', $subjects);
		
		$this->loadModel('Tgintakeorg');
		$tgintakeorgs = $this->Tgintakeorg->find('all' , array('recursive' => -1));
		$tgintakeorgslist = $this->Tgintakeorg->find('list');
		$this->set('tgintakeorgslist', $tgintakeorgslist);		
		
		// выбираем всех зачисленных
				
		$enrolledabiturients = $this->Enrolledstudent->find('all', array('conditions' => array('Enrolledstudent.profession_id' => $profession_id,
																									'Enrolledstudent.learningform_id' => $learningform_id,
																									'Enrolledstudent.learningtype_id' => $learningtype_id,
		)));
		
		$this->loadModel('Abiturient');
		
		// формируем массив id зачисленных
		$enrolled_ids = array();
		foreach($enrolledabiturients as $abiturient):
		array_push($enrolled_ids, $abiturient['Enrolledstudent']['abiturient_id']);
		endforeach;		
		

		// выбираем инфо по абитуриентам
			
		$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $enrolled_ids,
																												'Abiturient.prepared' => 1,
																												'Abiturient.approved' => 1,
																												'Abiturient.canceled' => 0),
																						'recursive' => 1,
																						'fields' => array ('Abiturient.id', 
																											'Abiturient.lastname', 
																											'Abiturient.firstname', 
																											'Abiturient.surname', 
																											'Abiturient.originaleducationdoc',)));		
			
		// считаем сумму баллов по специальности
		$score_total_abiturients = array(); // к данным абитуриента добавлен елемент Score_total
		foreach ($abiturients as $abiturient):
		// считаем  сумму балов по специальности
		$score_total = 0;
		foreach ($subjects_ids as $subject_id):
		foreach ($abiturient['Result'] as $result):
		if ($subject_id['SubjectsProfession']['subject_id'] == $result['subject_id']){
			$score_total += $result['score'];
		}
		endforeach;
		endforeach;
		$abiturient['Score_total'] = $score_total;
		array_push($score_total_abiturients, $abiturient);
		endforeach;

		// начинаем разбирать абитуриентов в  массив с сортровкой по рейтингу
		$plainrating_abiturients = array();
		foreach ($score_total_abiturients as $abiturient):
		$plainrating_abiturients[$abiturient['Abiturient']['id']] = $abiturient['Score_total'];
		endforeach;
		// сортируем массив целивиков по убыванию	Score_total
		arsort($plainrating_abiturients);
			
		$this->set('plainrating_abiturients',$plainrating_abiturients);
			
		$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $enrolled_ids,
																																		'Abiturient.prepared' => 1,
																																		'Abiturient.approved' => 1,
																																		'Abiturient.canceled' => 0,
																																		),
																			'recursive' => 1,
																			'fields' => array ('Abiturient.id', 
																								'Abiturient.lastname', 
																								'Abiturient.firstname', 
																								'Abiturient.surname', 
																								'Abiturient.originaleducationdoc',
																								'Abiturient.region_id')));
		$this->set('abiturients', $abiturients)	;	
		$this->render('/','blank','listenrolledfacultet');
	}
	/*
	 * выгрузка по зачисленным для банка
	 */
	function studentsforbanklist() {
		$facultet_id = $this->data['Enrolledstudent']['facultet_id'];
		$this->set('facultet_id', $facultet_id);
		$profession_id = $this->data['Enrolledstudent']['profession_id'];
		$this->set('profession_id', $profession_id);
		$learningform_id = $this->data['Enrolledstudent']['learningform_id'];
		$this->set('learningform_id', $learningform_id);
		$learningtype_id = $this->data['Enrolledstudent']['learningtype_id'];
		$this->set('learningtype_id', $learningtype_id);
		
		$this->set('facultet_id, profession_id, learningform_id, learningtype_id');
		
		$this->loadModel('Facultet');
		$facultets = $this->Facultet->find('list');
		$this->loadModel('Learningform');
		$learningforms = $this->Learningform->find('list');
		$this->loadModel('Learningtype');
		$learningtypes = $this->Learningtype->find('list');
		//, 'professions'
		$this->set(compact('learningtypes', 'learningforms', 'facultets'));
		
		$this->loadModel('Privilege');
		$privelegeslist = $this->Privilege->find('list');
		$this->set('privilegeslist',$privelegeslist);
		
		$this->loadModel('Profession');
		$professions = $this->Profession->find('list');
		$this->set(compact('professions'));
		
		$this->loadModel('Region');
		$regions = $this->Region->find('list');
		$this->set('regions', $regions);
		
		$this->loadModel('Gender');
		$genders = $this->Gender->find('list');
		$this->set('genders', $genders);

		$this->loadModel('Citizenship');
		$citizenships = $this->Citizenship->find('list');
		$this->set('citizenships', $citizenships);		
		// выбираем всех зачисленных
		
		$enrolledabiturients = $this->Enrolledstudent->find('all', array('conditions' => array('Enrolledstudent.profession_id' => $profession_id,
																											'Enrolledstudent.learningform_id' => $learningform_id,
																											'Enrolledstudent.learningtype_id' => $learningtype_id,
		)));
		
		$this->loadModel('Abiturient');
		
		// формируем массив id зачисленных
		$enrolled_ids = array();
		foreach($enrolledabiturients as $abiturient):
		array_push($enrolled_ids, $abiturient['Enrolledstudent']['abiturient_id']);
		endforeach;
		

			
		$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $enrolled_ids,
																				'Abiturient.prepared' => 1,
																				'Abiturient.approved' => 1,
																				'Abiturient.canceled' => 0,),
																			'order' => 'Abiturient.lastname',
																			));
		$this->set('abiturients', $abiturients)	;
		$this->render('/','blank','studentsforbanklist');		
	}
	
	/*
	 * выгрузка списка зачисленных по факультету для импорта в канцелярию
	 */
	function makecsvfile() {
		Configure::write('debug', 0);
		// получам id факультета специальности формы и типа обучения
		$facultet_id = $this->data['Enrolledstudent']['facultet_id'];
		// $this->set('facultet_id', $facultet_id);
		$profession_id = $this->data['Enrolledstudent']['profession_id'];
		// $this->set('profession_id', $profession_id);
		$learningform_id = $this->data['Enrolledstudent']['learningform_id'];
		// $this->set('learningform_id', $learningform_id);
		$learningtype_id = $this->data['Enrolledstudent']['learningtype_id'];
		// $this->set('learningtype_id', $learningtype_id);
		
		// выбираем всех зачисленных
		
		$enrolledabiturients = $this->Enrolledstudent->find('all', array('conditions' => array('Enrolledstudent.profession_id' => $profession_id,
																													'Enrolledstudent.learningform_id' => $learningform_id,
																													'Enrolledstudent.learningtype_id' => $learningtype_id,
		)));
		
		// формируем массив id зачисленных
		$enrolled_ids = array();
		foreach($enrolledabiturients as $abiturient):
		array_push($enrolled_ids, $abiturient['Enrolledstudent']['abiturient_id']);
		endforeach;
		
		$this->loadModel('Abiturient');
		$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $enrolled_ids,), 'recursive' => 1));
		$this->set('abiturients', $abiturients)	;
		$this->render('/','blank','makecsvfile');
	}
	
	function makemoreinfofile () {
		Configure::write('debug', 0);
		$this->loadModel('Profession');
		$listprofessions = $this->Profession->find('list');
		$this->set(compact('listprofessions'));
		$professions = $this->Profession->find('all', array('recursive' => -1));
		//debug($professions);
		$allinfo = array();
		foreach ($professions as $profession):
			$professioninfo = array();
		$professioninfo ['profession_id'] = $profession['Profession']['id'];
			$students = $this->Enrolledstudent->find('all', array('conditions' => array('Enrolledstudent.profession_id' => $profession['Profession']['id'])));
			$tmpprofessioninfo = array();
			foreach ($students as $student):
				array_push($tmpprofessioninfo, array('fio' => $student['Abiturient']['lastname'].' '.$student['Abiturient']['firstname'].' '.$student['Abiturient']['surname'], 'moreinfo' => $student['Abiturient']['moreinfo']));
			endforeach;
			array_push($professioninfo,array('students' => $tmpprofessioninfo));
			array_push($allinfo, $professioninfo);
		endforeach;
		//debug($allinfo);
		$this->set('allinfo', $allinfo);
		$this->render('/','blank','makemoreinfofile');
	}
}
?>